﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace T.FAS.Runtime.Metadata.Entity.DataModel
{
    /// <summary>
    /// 关联
    /// </summary>
    [Serializable]
    public class Association : System.ICloneable
    {
        /// <summary>
        /// 标识
        /// </summary>
        public string ID { get; set; }

        /// <summary>
        /// 关联的名称
        /// </summary>
        /// <remarks>
        /// 主要用于显示和按关联取数
        /// </remarks>
        public string Name { get; set; }

        /// <summary>
        /// 领域模型ID
        /// </summary>
        [JsonIgnore]
        public string CompositeDataObjectID { get; set; }

        /// <summary>
        /// 领域对象ID
        /// </summary>
        [JsonIgnore]
        public string CompositeNodeObjectID { get; set; }

        #region InnerJoinItem 主子关联项
        /// <summary>
        /// 关联的领域模型ID
        /// </summary>
        public string AssoCompositeDataObjectID { get; set; }

        //[NonSerialized]
        //[JsonIgnore]
        private CompositeDataObject assoDomaiModel;

        /// <summary>
        /// 关联的领域模型ID，json序列化忽略，在CDO中重新构建引用
        /// </summary>        
        [JsonIgnore]
        [JsonProperty(ReferenceLoopHandling = ReferenceLoopHandling.Ignore)]
        public CompositeDataObject AssoDomaiModel
        {
            get
            {
                return assoDomaiModel;
            }
            set
            {
                assoDomaiModel = value;
                if (value != null)
                {
                    AssoCompositeDataObjectID = value.ID;
                }
            }
        }

        /// <summary>
        /// 关联的领域对象ID
        /// </summary>
        public string AssoCompositeNodeObjectID { get; set; }

        //[NonSerialized]
        //[JsonIgnore]        
        private CompositeNodeObject assoCompositeNodeObject;

        /// <summary>
        /// 关联的领域对象，json序列化忽略，在CDO中重新构建引用
        /// </summary>
        [JsonIgnore]
        [JsonProperty(ReferenceLoopHandling = ReferenceLoopHandling.Ignore)]
        public CompositeNodeObject AssoCompositeNodeObject
        {
            get
            {
                return assoCompositeNodeObject;
            }
            set
            {
                assoCompositeNodeObject = value;

                if (value != null)
                    AssoCompositeNodeObjectID = value.ID;
            }
        }


        #endregion

        #region LeftJoinItem 外键关联项 
        /// <summary>
        /// 外键关联数据对象ID
        /// </summary>
        public string AssoDataObjectID { get; set; }

        /// <summary>
        /// 外键关联数据对象Table Alias
        /// </summary>
        public string AssoDataObjectTableAlias { get; set; }

        /// <summary>
        /// 外键关联数据对象实体
        /// </summary>
        public DataObject AssoDataObject { get; set; }

        #endregion

        /// <summary>
        /// 关联类型
        /// </summary>
        public AssociateType AssociateType { get; set; }

        /// <summary>
        /// 关联上的设置的过滤条件
        /// </summary>        
        public string FilterCondition { get; set; }

        //关联项
        private List<AssociateItem> items;

        /// <summary>
        /// 关联项
        /// </summary>
        public List<AssociateItem> Items
        {
            get
            {
                if (items == null)
                    items = new List<AssociateItem>();

                return items;
            }
            set
            {
                items = value;
            }
        }

        /// <summary>
        /// 属性名称
        /// </summary>
        public string PropertyName { get; set; }

        /// <summary>
        /// 属性类型
        /// </summary>
        public string PropertyType { get; set; }

        /// <summary>
        /// 是否懒加载
        /// </summary>
        public bool IsLazyLoad { get; set; }

        //关联带出的元素
        private List<AssociationRefElement> refElements;

        /// <summary>
        /// 关联带出的元素
        /// </summary>
        public List<AssociationRefElement> RefElements
        {
            get
            {
                if (refElements == null)
                {
                    refElements = new List<AssociationRefElement>();
                }

                return refElements;
            }
            set
            {
                refElements = value;
            }
        }

        /// <summary>
        /// 创建人
        /// </summary>
        //[DataMember]
        [JsonIgnore]
        public string Creator { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>        
        //[DataMember]
        [JsonIgnore]
        public DateTime CreateTime { get; set; }

        /// <summary>
        /// 最后修改人
        /// </summary>
        //[DataMember]
        [JsonIgnore]
        public string LastModifier { get; set; }

        /// <summary>
        /// 最后修改时间
        /// </summary>        
        //[DataMember]
        [JsonIgnore]
        public DateTime LastModifyTime { get; set; }

        public object Clone()
        {
            var association = this.MemberwiseClone() as Association;

            if (RefElements != null)
            {
                association.RefElements = new List<AssociationRefElement>();
                RefElements.ForEach(i=> association.RefElements.Add(i));
            }

            return association;
        }
    }
}
